﻿@page "/"
@using System.Security.Cryptography;
@using System.Text; 

<h1>Hd Wallet</h1>
<EditForm Model=@WalletViewModel OnSubmit=@GetAccounts>
    <InputTextArea @bind-Value=WalletViewModel.Words />
    <input type="submit" value="Submit" class="btn btn-primary" />
</EditForm>

@if (WalletViewModel.Accounts != null && WalletViewModel.Accounts.Count > 0)
{
    <table>
        <thead>
            <tr>Account</tr>
            <tr>Private Key</tr>
        </thead>
        <tbody>
            @foreach (var account in WalletViewModel.Accounts)
            {
              <tr>
                <td>@account.Address</td>
                <td> @account.PrivateKey</td>
               </tr>
            }
        </tbody>

    </table>
}


@code {

    public class WalletVM
    {
        public string Words { get; set; } = "ripple scissors kick mammal hire column oak again sun offer wealth tomorrow wagon turn fatal";
        public List<Nethereum.Web3.Accounts.Account> Accounts = new List<Nethereum.Web3.Accounts.Account>();

    }

    public WalletVM WalletViewModel = new WalletVM();

    private void GetAccounts()
    {
        TestSha256();

        // sign something
        var signer = new Nethereum.Signer.MessageSigner();
        var signedMessage = signer.HashAndSign("something something", "b5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7");

        Console.WriteLine(signedMessage);
        Console.WriteLine(signer.HashAndEcRecover("something something", signedMessage));


        var signerEthereum = new Nethereum.Signer.EthereumMessageSigner();
        var signedMessageEthereum = signerEthereum.HashAndSign("something something", "b5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7");

        Console.WriteLine(signedMessageEthereum);
        Console.WriteLine(signerEthereum.HashAndEcRecover("something something", signedMessageEthereum));


        var wallet = new Nethereum.HdWallet.Wallet(WalletViewModel.Words, null);
        WalletViewModel.Accounts.Clear();

        for (int i = 0; i < 10; i++)
        {
            WalletViewModel.Accounts.Add(wallet.GetAccount(i));
            this.StateHasChanged();
        }

    }


    public static void TestSha256()
    {
        string source = "Hello World!";
        using (SHA256 sha256Hash = SHA256.Create())
        {
            string hash = GetHash(sha256Hash, source);

            Console.WriteLine($"The SHA256 hash of {source} is: {hash}.");

            Console.WriteLine("Verifying the hash...");

            if (VerifyHash(sha256Hash, source, hash))
            {
                Console.WriteLine("The hashes are the same.");
            }
            else
            {
                Console.WriteLine("The hashes are not same.");
            }
        }
    }

    private static string GetHash(HashAlgorithm hashAlgorithm, string input)
    {

        // Convert the input string to a byte array and compute the hash.
        byte[] data = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(input));

        // Create a new Stringbuilder to collect the bytes
        // and create a string.
        var sBuilder = new StringBuilder();

        // Loop through each byte of the hashed data
        // and format each one as a hexadecimal string.
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        // Return the hexadecimal string.
        return sBuilder.ToString();
    }

    // Verify a hash against a string.
    private static bool VerifyHash(HashAlgorithm hashAlgorithm, string input, string hash)
    {
        // Hash the input.
        var hashOfInput = GetHash(hashAlgorithm, input);

        // Create a StringComparer an compare the hashes.
        StringComparer comparer = StringComparer.OrdinalIgnoreCase;

        return comparer.Compare(hashOfInput, hash) == 0;
    }

}


